package com.aj.controller;

import com.aj.Interface.MemberService;
import com.aj.common.MessageConstant;
import com.aj.pojo.CheckItem;
import com.aj.pojo.Member;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginControler {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;

    //登录
    @RequestMapping("/login")
    public Result login(@RequestBody Map map) {
        try {
            String telephone = (String) map.get("telephone");
            String validateCode = (String) map.get("validateCode");
            String jedisCode = jedisPool.getResource().get(telephone);
            if (jedisCode==null||!validateCode.equals(jedisCode)){
                return new Result(false,MessageConstant.VALIDATECODE_ERROR);
            }
            //查询是否是会员
       Member member = memberService.findBytelephone(telephone);
            if (member==null){
                member=new Member();
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                memberService.addmember(member);
            }
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
    }
}
